<?php
class SalleController extends Controller {
	protected $salle;
	private $topSalle;
	private $topMessage;
	
	public function __construct() {
		$this->topSalle = Loader::loadModel('Salle');
		$this->topSalle = Salle::getTopSalles();
		$this->topMessage = Loader::loadModel('Message');
		$this->topMessage = Message::get10MostLikedMessages();
	}
	
	public function viewSalle($id) {
		$this->salle = Loader::loadModel('Salle');
		$this->salle = Salle::getSalle($id);
		$isPost = ParameterManager::getInstance()->getPost('POSTMESSAGE');
		if(isset($isPost)) {
			$titre = ParameterManager::getInstance()->getPost('TITRE');
			$message = ParameterManager::getInstance()->getPost('MESSAGE');
			Loader::loadModel('Message');
			$userController = Loader::loadController('User');
			$user = $userController->getUserSessionInfos();
			if(!empty($user))
				Message::creerMessage($id, $userController->getUserSessionInfos()->getId(), $titre, $message);
		}
		
		$isCommented = ParameterManager::getInstance()->getPost('COMMENTPOST');
		if(isset($isCommented)) {
			$nomparent = ParameterManager::getInstance()->getPost('TITRE');
			$titre="Re:".$nomparent;
			$message = ParameterManager::getInstance()->getPost('COMMENT');
			$parent = ParameterManager::getInstance()->getPost('PARENT');
			Loader::loadModel('Message');

			$userController = Loader::loadController('User');
			$user = $userController->getUserSessionInfos();
			if(!empty($user))
				Message::creerMessage($id, $userController->getUserSessionInfos()->getId(), $titre, $message, $parent);
		}
		
	}
	
	public function viewfilter() {
		$args = (ParameterManager::getInstance()->getParam('args'));
		$userController = Loader::loadController('User');
		if($userController->isConnected()) {
			$connectedUsers = Loader::loadModel('User');
			$connectedUsers = User::getConnectedUsers();
			
			if(is_array($args) && isset($args[0])) {	
				$this->salle = Loader::loadModel('Salle');
				$this->salle = Salle::getSallesWithTheme($args[0]);
			} 
			else {
				$this->salle = Loader::loadModel('Salle');
				$this->salle = Salle::getAllSalles($userController->getUserSessionInfos()->getId());
			}
			if($userController->getUserSessionInfos()->getStatusId()!=1)
				$view = Loader::loadView('View',
										array('top'=>'topUserLogged','left'=>'leftColumn','center'=>'viewAllSalles'));
			else
				$view = Loader::loadView('View',
										array('top'=>'topAdmin','left'=>'leftColumn','center'=>'viewAllSalles'));
			$view->setParam('salles',$this->salle);
			$view->setParam('topmessages',$this->topMessage);
			$view->setParam('connectedUsers', $connectedUsers);
			$view->setParam('user',$userController->getUserSessionInfos());
		}
		else {
			if(is_array($args) && isset($args[0])) {	
				$this->salle = Loader::loadModel('Salle');
				$this->salle = Salle::getSallesWithTheme($args[0]);
			} 
			else {
				$this->salle = Loader::loadModel('Salle');
				$this->salle = Salle::getLastPublicSalles();
			}
			$view = Loader::loadView('View',
									array('top'=>'topUserNotLogged','left'=>'leftColumnNotLogged','center'=>'viewAllSalles'));
			$view->setParam('salles',$this->salle);
		}
		$view->setParam('topsalles',$this->topSalle);
		$view->setParam('topmessages',$this->topMessage);
		$view->render();
	} 
	
	public function viewMine() {
		$userController = Loader::loadController('User');
		if($userController->isConnected()) {
			
			$isOnDeleteAction = ParameterManager::getInstance()->getPost('DELETESALLE');
			if($isOnDeleteAction) {
				$idSalle = ParameterManager::getInstance()->getPost('IDSALLE');
				Loader::loadModel('Salle');
				Salle::getSalle($idSalle)->supprimerSalle();
			}
			
			$connectedUsers = Loader::loadModel('User');
			$connectedUsers = User::getConnectedUsers();
			$this->salle = Loader::loadModel('Salle');
			$this->salle = Salle::getSallesofUser($userController->getUserSessionInfos()->getId());
			if($userController->getUserSessionInfos()->getStatusId()!=1)
				$view = Loader::loadView('View',
										array('top'=>'topUserLogged','left'=>'leftColumn','center'=>'viewMySalles'));
			else
				$view = Loader::loadView('View',
										array('top'=>'topAdmin','left'=>'leftColumn','center'=>'viewMySalles'));
			$view->setParam('salles',$this->salle);
			$view->setParam('topsalles',$this->topSalle);
			$view->setParam('topmessages',$this->topMessage);
			$view->setParam('connectedUsers', $connectedUsers);
			$view->setParam('user',$userController->getUserSessionInfos());
			$view->render();
		} else {
			$view = Loader::loadView('View', 
									array('top'=>'topUserNotLogged','left'=>'leftColumnNotLogged','center'=>'restrictedPage'));
			$view->setParam('topsalles',$this->topSalle);
			$view->setParam('topmessages',$this->topMessage);
			$view->render();
		}
	}
	
	public function view($page="viewSalle",$param=null) {
		$args = (ParameterManager::getInstance()->getParam('args'));
		$userController = Loader::loadController('User');
		if(is_array($args) && isset($args[0])) {
			$this->viewSalle($args[0]);
			if(!$this->salle->isEmpty()) {
				if($userController->isConnected()) {
					$connectedUsers = Loader::loadModel('User');
					$connectedUsers = User::getConnectedUsers();
					if($userController->getUserSessionInfos()->getStatusId()!=1)
						$view = Loader::loadView('View', 
													array('title'=>$this->salle->getNom(),'top'=>'topUserLogged','left'=>'leftColumn','center'=>'viewSalle'));
					else
						$view = Loader::loadView('View', 
													array('title'=>$this->salle->getNom(),'top'=>'topAdmin','left'=>'leftColumn','center'=>'viewSalle'));
					$view->setParam('salle',$this->salle);
					$view->setParam('user',$userController->getUserSessionInfos());
					$view->setParam('topsalles',$this->topSalle);
					$view->setParam('topmessages',$this->topMessage);
					$view->setParam('connectedUsers', $connectedUsers);
					$view->render();
				} else {
					$view = Loader::loadView('View', 
												array('title'=>$this->salle->getNom(),'top'=>'topUserNotLogged','left'=>'leftColumnNotLogged','center'=>'viewSalle'));
					$view->setParam('topsalles',$this->topSalle);
					$view->setParam('topmessages',$this->topMessage);
					$view->setParam('salle',$this->salle);
					$view->render();
				}
				
			} else {
				$this->viewfilter();
			}
		} else {
			$this->viewfilter();
		}
	}
	
	public function create() {
		$userController = Loader::loadController('User');
		if($userController->isConnected()) {
			$connectedUsers = Loader::loadModel('User');
			$connectedUsers = User::getConnectedUsers();
			$menu = 'topUserLogged';
			if($userController->getUserSessionInfos()->getStatusId()!=1)
				$menu = 'topUserLogged';
			else
				$menu = 'topAdmin';
			
			$isPost = ParameterManager::getInstance()->getPost('POSTSALLE');
			if(isset($isPost)) {
				$nomSalle = ParameterManager::getInstance()->getPost('NOMSALLE');
				$themeSalle = ParameterManager::getInstance()->getPost('THEMES');
				$groupeSalle = ParameterManager::getInstance()->getPost('GROUPES');
				$message = ParameterManager::getInstance()->getPost('MESSAGE');
				if(!is_array($themeSalle))
					$themeSalle = array();
				if(!is_array($groupeSalle))
					$groupeSalle = array();
				$userController->getUserSessionInfos()->creerSalle($nomSalle,$themeSalle,$groupeSalle,$message);
			}
			$themes = Loader::loadModel('Theme');
			$themes = Theme::getAllThemes();
			
			$groupes = Loader::loadModel('Groupe');
			$groupes = Groupe::getGroupesofUser($userController->getUserSessionInfos()->getId());
			if(isset($isPost))
				$view = Loader::loadView('View', 
										array('title'=>'Créer une salle','top'=>$menu,'left'=>'leftColumn','center'=>'salleCreated'));
			else
				$view = Loader::loadView('View', 
										array('title'=>'Créer une salle','top'=>$menu,'left'=>'leftColumn','center'=>'createSalle'));
			$view->setParam('salle',$this->salle);
			$view->setParam('user',$userController->getUserSessionInfos());
			
			$view->setParam('topsalles',$this->topSalle);
			$view->setParam('topmessages',$this->topMessage);
			$view->setParam('connectedUsers', $connectedUsers);
			
			$view->setParam('themes', $themes);
			$view->setParam('groupes',$groupes);
			$view->render();
		} else {
			$view = Loader::loadView('View', 
										array('title'=>'Page restreinte','top'=>'topUserNotLogged','left'=>'leftColumnNotLogged','center'=>'restrictedPage'));
			$view->setParam('topsalles',$this->topSalle);
			$view->setParam('topmessages',$this->topMessage);
			$view->setParam('salle',$this->salle);
			$view->render();
		}
				
	}
	
	public function modify() {
		$userController = Loader::loadController('User');
		if($userController->isConnected()) {
			$connectedUsers = Loader::loadModel('User');
			$connectedUsers = User::getConnectedUsers();
			$menu = 'topUserLogged';
			if($userController->getUserSessionInfos()->getStatusId()!=1)
				$menu = 'topUserLogged';
			else
				$menu = 'topAdmin';
			
			$themes = Loader::loadModel('Theme');
			$themes = Theme::getAllThemes();
			
			$groupes = Loader::loadModel('Groupe');
			$groupes = Groupe::getGroupesofUser($userController->getUserSessionInfos()->getId());
			
			$idSalle = ParameterManager::getInstance()->getPost('IDSALLE');
			if(!isset($idSalle))
				$view = Loader::loadView('View', 
										array('title'=>'Modifier une salle','top'=>$menu,'left'=>'leftColumn','center'=>'salleCreated'));
			else {
				$view = Loader::loadView('View', 
										array('title'=>'Modifier une salle','top'=>$menu,'left'=>'leftColumn','center'=>'modifySalle'));
				$this->salle = Loader::loadModel('Salle');
				$this->salle = Salle::getSalle($idSalle);
			}
			
			$idSalleInModif = ParameterManager::getInstance()->getPost('IDSALLE_MODIF');
			if(isset($idSalleInModif)) {
				$nomSalle = ParameterManager::getInstance()->getPost('NOMSALLE');
				$themeSalle = ParameterManager::getInstance()->getPost('THEMES');
				$groupeSalle = ParameterManager::getInstance()->getPost('GROUPES');
				
				$themes = Loader::loadModel('Theme');
				$groupes = Loader::loadModel('Groupe');
				if(!is_array($themeSalle))
					$themeSalle = array();
				if(!is_array($groupeSalle))
					$groupeSalle = array();
					
				$userController->getUserSessionInfos()->modifierSalle($idSalleInModif, $nomSalle,$themeSalle,$groupeSalle);
			}
			$view->setParam('salle',$this->salle);
			$view->setParam('user',$userController->getUserSessionInfos());
			
			$view->setParam('topsalles',$this->topSalle);
			$view->setParam('topmessages',$this->topMessage);
			$view->setParam('connectedUsers', $connectedUsers);
			
			$view->setParam('themes', $themes);
			$view->setParam('groupes',$groupes);
			$view->render();
		} else {
			$view = Loader::loadView('View', 
										array('title'=>'Page restreinte','top'=>'topUserNotLogged','left'=>'leftColumnNotLogged','center'=>'restrictedPage'));
			$view->setParam('topsalles',$this->topSalle);
			$view->setParam('topmessages',$this->topMessage);
			$view->setParam('salle',$this->salle);
			$view->render();
		}
				
	}
	
	public function likePost() {
		$userController = Loader::loadController('User');
		if($userController->isConnected()) {
			$idmessage = ParameterManager::getInstance()->getQuery('idpost');
			$message = Loader::loadModel('Message');
			$message = Message::getMessageFromId($idmessage);
			$message->likePost($userController->getUserSessionInfos()->getId());
		}
	}
	
	public function unlikePost() {
		$userController = Loader::loadController('User');
		if($userController->isConnected()) {
			$idmessage = ParameterManager::getInstance()->getQuery('idpost');
			$message = Loader::loadModel('Message');
			$message = Message::getMessageFromId($idmessage);
			$message->unlikePost($userController->getUserSessionInfos()->getId());
		}
	}

	
	public function defaut() {
		$this->viewfilter();
	}
}